/*
 * Copyright © 2009 Gerald Isaac Schwarz
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package base;

import java.util.ArrayList;

public class Statistics {
	private Engine engine;
	
	public Statistics(Engine engine) {
		this.engine = engine;
	}
	
	public Engine getEngine() {
		return this.engine;
	}
	
	public VirtualBoard getBoard() {
		return this.engine.getBoard();
	}
	
	/**
	 * 
	 * @param piece
	 * @return The number of times the piece has moved during the game.
	 */
	public int getMoveCountByPiece(Piece piece) {
		int count = 0;
		State lastMove = piece.getInitialState();
		
		for (Turn turn : this.getChangesByPiece(piece)) {
			for (State state : turn.getChanges()) {
				if (state.isAlive() && lastMove.isAlive() && !state.getLocation().equals(lastMove.getLocation())) {
					count++;
					lastMove = state;
				}
			}
		}
		
		return count;
	}
	
	/**
	 * 
	 * @param piece
	 * @return The number of times the piece has been dropped during the game.
	 */
	public int getDropCountByPiece(Piece piece) {
		int count = 0;
		State lastMove = piece.getInitialState();
		
		for (Turn turn : this.getChangesByPiece(piece)) {
			for (State state : turn.getChanges()) {
				if (state.isAlive() && !lastMove.isAlive()) {
					count++;
					lastMove = state;
				}
			}
		}
		
		return count;
	}
	
	public ArrayList<Turn> getTurnsByPiece(Piece piece) {
		ArrayList<Turn> turns = new ArrayList<Turn>();
		
		for (Turn turn : this.getBoard().getTurns()) {
			for (State state : turn.getChanges()) {
				if (state.getPiece() == piece) {
					turns.add(turn);
					break;
				}
			}
		}
		
		return turns;
	}
	
	public ArrayList<Turn> getChangesByPiece(Piece piece) {
		ArrayList<Turn> turns = new ArrayList<Turn>();
		
		for (Turn turn : this.getBoard().getTurns()) {
			ArrayList<State> change = new ArrayList<State>();
			
			for (State state : turn.getChanges()) {
				if (state.getPiece() == piece) {
					change.add(state);
				}
			}
			
			turns.add(new Turn(change));
		}
		
		return turns;
	}
}
